#ifndef __CONFIG_GET_H__
#define __CONFIG_GET_H__

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

static inline int config_get(const char *file, const char *key, char *value, int len)
{
    int ret = -1;
    char line[512];
    FILE *fp = fopen(file, "r");
    if (!fp) { syslog(LOG_ERR, "fail to open file %s[%m]\n", file); return -1; }

    int name_len = strlen(key);
    while (fgets(line, sizeof(line), fp)) {
        if (strncmp(line, key, name_len) == 0) {
            if (line[name_len] != ' ' && line[name_len] != '=') continue;
            char *p = strchr(line, '=');
            if (!p) continue;
            p++;
            while (*p == ' ') p++;
            int i = 0;
            while(*p) {
                if (i >= len) goto out;
                if (*p == '\r' || *p == '\n') break;
                value[i] = *p;
                i++;
                p++;
            }
            value[i] = 0;
            ret = 0;
            goto out;
        }
    }
    syslog(LOG_ERR, "fail to get config %s!!\n", key);
out:
    if (fp) fclose(fp);
    return ret;
}

#endif
